package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.enums.StatusCode;
import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.pojo.vo.PageData;
import cn.tedu.ivos.base.utils.PageInfoToPageDataConverter;
import cn.tedu.ivos.dict.pojo.vo.DictVO;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQueryParam;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        String username=userLoginParam.getUsername();
        String password=userLoginParam.getPassword();
        //1.判断参数的合法性
        if(username==null||"".equals(username))
            throw new IllegalArgumentException("用户名不能为空");
        if(password==null||"".equals(password))
            throw new IllegalArgumentException("密码不能为空");
        //2.基于用户名查询用户信息
        UserVO userVO = userMapper.selectByUsername(username);
        if(userVO==null)
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        //3.判断密码是否正确
        if(!password.equals(userVO.getPassword()))
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        //4.存储用户的登录状态
        ServletRequestAttributes requestAttributes=
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpSession session = request.getSession(true);//HttpSession
        //session.setMaxInactiveInterval(60*60*24);
        System.out.println("sessionId="+session.getId());
        System.out.println(userVO);
        session.setAttribute("user",userVO);
        return userVO;
    }

  /*  @Override
    public List<UserVO> selectUserList(UserQueryParam userQuery) {
        List<UserVO> list= userMapper.selectUserList(userQuery);
        return list;
    }*/

    @Override
    public PageData<UserVO> selectUserList(UserQueryParam userQuery) {
        PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());
        List<UserVO> list = userMapper.selectUserList(userQuery);
        PageInfo<UserVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        User user=new User();
        //将参数对象内容拷贝到实体对象中
        BeanUtils.copyProperties(userSaveParam,user);
        if(user.getId()==null){
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setPassword("123456");
            userMapper.insert(user);
        }else{
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public void updateUser(UserSaveParam userSaveParam) {
        User user=new User();
        //将参数对象内容拷贝到实体对象中
        BeanUtils.copyProperties(userSaveParam,user);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void resetPassword(Long userId) {
        User user=new User();
        user.setId(userId);
        user.setUpdateTime(new Date());
        user.setPassword("123456");
        userMapper.update(user);
    }

    @Override
    public void changePassword(Long userId, String status) {
        User user=new User();
        user.setId(userId);
        user.setUpdateTime(new Date());
        user.setStatus(status);
        userMapper.update(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public UserVO selectById(Long id) {
        return userMapper.selectById(id);
    }


    @Override
    public List<UserVO> selectAuditList(Long parentId) {
        List<UserVO> list=new ArrayList<>();
        UserVO userVO1 = userMapper.selectById(parentId);
        if(userVO1!=null){
            list.add(userVO1);
        }
        if(userVO1.getParentId()!=null){
            UserVO userVO2=userMapper.selectById(userVO1.getParentId());
            if(userVO2!=null) list.add(userVO2);
        }
        return list;
    }
}
